package com.github.binarywang.demo.spring.handler;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.demo.spring.builder.AbstractBuilder;
import com.github.binarywang.demo.spring.builder.ImageBuilder;
import com.github.binarywang.demo.spring.builder.TextBuilder;
import com.github.binarywang.demo.spring.dto.WxMenuKey;
import com.github.binarywang.demo.spring.service.WeixinService;

import io.renren.utils.Config;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.builder.outxml.NewsBuilder;

/**
 * 
 * @author Binary Wang
 *
 */
@Component
public class MenuHandler extends AbstractHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());
  @Override
  public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
      Map<String, Object> context, WxMpService wxMpService,
      WxSessionManager sessionManager) {
    WeixinService weixinService = (WeixinService) wxMpService;

    String key = wxMessage.getEventKey();
   logger.info("key="+key);

    try {
    	   switch (key) {
    	    case "预订车位":
    	    	return replyNews(wxMessage);
    	    case "历史记录":
    	    	return useList(wxMessage);
    	    case "个人主页":
    	        return wxUser(wxMessage);
    	    default:
    	    	 return WxMpXmlOutMessage.TEXT().content("无法解析指令")
    	    	          .fromUser(wxMessage.getToUser())
    	    	          .toUser(wxMessage.getFromUser()).build();
    	    }
     
    } catch (Exception e) {
    	
      return WxMpXmlOutMessage.TEXT().content("错误异常")
          .fromUser(wxMessage.getToUser())
          .toUser(wxMessage.getFromUser()).build();
    }
    
  


 

  }

private WxMpXmlOutNewsMessage replyNews(WxMpXmlMessage wxMessage) {
	NewsBuilder newsBuilder=new NewsBuilder();
	  String testUrl =  Config.getHost()+Config.getPath()+"/Test/index.html";
	  logger.debug("host:{}", Config.getHost());
	  logger.debug("path:{}", Config.getPath());
	WxMpXmlOutNewsMessage.Item item=new WxMpXmlOutNewsMessage.Item(); 
	item.setDescription("测试");
	item.setPicUrl(Config.getHost()+Config.getPath()+"/statics/car-apply-ESDF.jpg");
	item.setTitle("测试页面");
	item.setUrl(testUrl);

	return newsBuilder.addArticle(item).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
}

private WxMpXmlOutNewsMessage wxUser(WxMpXmlMessage wxMessage) {
	NewsBuilder newsBuilder=new NewsBuilder();
	  String testUrl =  Config.getHost()+Config.getPath()+"/Tests/user.html";
	  logger.debug("host:{}", Config.getHost());
	  logger.debug("path:{}", Config.getPath());
	WxMpXmlOutNewsMessage.Item item=new WxMpXmlOutNewsMessage.Item(); 
	item.setDescription("测试11111");
	item.setPicUrl(Config.getHost()+Config.getPath()+"/statics/car-apply-ESDF.jpg");
	item.setTitle("测试页面222222");
	item.setUrl(testUrl);

	return newsBuilder.addArticle(item).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
}

private WxMpXmlOutNewsMessage useList(WxMpXmlMessage wxMessage){
	
	NewsBuilder newsBuilder=new NewsBuilder();
	  String testUrl =  Config.getHost()+Config.getPath()+"/Tests/lists";
	  logger.debug("host:{}", Config.getHost());
	  logger.debug("path:{}", Config.getPath());
	WxMpXmlOutNewsMessage.Item item=new WxMpXmlOutNewsMessage.Item(); 
	item.setDescription("测试测试用户");
	item.setPicUrl(Config.getHost()+Config.getPath()+"/statics/car-apply-ESDF.jpg");
	item.setTitle("测试用户页面");
	item.setUrl(testUrl);

	return newsBuilder.addArticle(item).fromUser(wxMessage.getToUser()).toUser(wxMessage.getFromUser()).build();
	
    }
}
